//
//  HomeSwitchFocusOnUsersController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/18.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeSwitchFocusOnUsersController.h"
#import "HomeFocusCell.h"
#import "HomeSwitchFocusModel.h"
#import "PersonalHomePageVideoCell.h"

@interface HomeSwitchFocusOnUsersController ()
<UICollectionViewDelegate,UICollectionViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;
@end

@implementation HomeSwitchFocusOnUsersController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: self.view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15,15)];
    //创建 layer
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.view.bounds;
    //赋值
    maskLayer.path = maskPath.CGPath;
    self.view.layer.mask = maskLayer;
    
    [self.view addSubview:self.listView];

}

#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeSwitchFocusOnUsersCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(HomeSwitchFocusOnUsersCell.class) forIndexPath:indexPath];
    return cell;
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(KScreenWidth, KScreenHeight-44);
}
// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

//cell将要出现的时候调用
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
//    DLog(@"%@",indexPath);
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    DLog(@"%@",indexPath);
    DLog(@"%@",collectionView.visibleCells);
    DLog(@"%@",collectionView.indexPathsForVisibleItems);
    NSIndexPath * path = collectionView.indexPathsForVisibleItems.lastObject;
    [self postNot:path.row];

}
-(void)postNot:(NSInteger)index{
    [[NSNotificationCenter defaultCenter] postNotificationName:K_ChangePageControl object:@{@"index":@(index)}];
}
#pragma mark ---懒加载
-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        self.layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;

        _listView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-44) collectionViewLayout:self.layout];
        _listView.backgroundColor=KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        _listView.scrollEnabled = YES;
        _listView.pagingEnabled = YES;
        _listView.showsHorizontalScrollIndicator=NO;
        _listView.showsVerticalScrollIndicator=NO;
        _listView.bounces = NO;

        [_listView registerClass:[HomeSwitchFocusOnUsersCell class] forCellWithReuseIdentifier:NSStringFromClass([HomeSwitchFocusOnUsersCell class])];
    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        NSArray * onearray = @[@"",@"",@"",@"",@""];
        _dataArray =[[NSMutableArray alloc]init];
        [_dataArray addObjectsFromArray:onearray];
    }
    return _dataArray;
}

@end


@interface HomeSwitchFocusOnUsersCell ()<UITableViewDelegate,UITableViewDataSource>
/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataArray;
@end

@implementation HomeSwitchFocusOnUsersCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
//        UIView * view = UIView.new;
//        view.backgroundColor = KRedColor;
//        view.frame = CGRectMake(0, 100, 100, 100);
//        [self addSubview:view];
        [self addSubview:self.listView];
        [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
    }
    return self;
}

#pragma mark ---UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeFocusCell * oneImageCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeFocusCell class])];
    HomeFocusTwoImageCell * twoImageCell =[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeFocusTwoImageCell class])];
    PersonalHomePageVideoCell * videoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalHomePageVideoCell class])];
    
    HomeSwitchFocusModel * model = self.dataArray[indexPath.section];
    if (model.type == HomeSwitchFocusText) {
        
    }
    if (model.type == HomeSwitchFocusTextAndImage) {
        if (model.imageNum==1) {
            oneImageCell.switchModel = model;
            return oneImageCell;
        }
        if (model.imageNum == 2) {
            twoImageCell.switchModel = model;
            return twoImageCell;
        }
    }
    if (model.type == HomeSwitchFocusTextAndVideo) {
        videoCell.swithModel = model;
        return videoCell;
    }

    return oneImageCell;
}
#pragma mark --懒加载
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView registerClass:[HomeFocusCell class] forCellReuseIdentifier:NSStringFromClass([HomeFocusCell class])];
        [_listView registerClass:[HomeFocusTwoImageCell class] forCellReuseIdentifier:NSStringFromClass([HomeFocusTwoImageCell class])];
        [_listView registerClass:[PersonalHomePageVideoCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageVideoCell class])];

        _listView.bounces = NO;

    }
    return _listView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"HomeSwitchFocus" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            HomeSwitchFocusModel *model = [HomeSwitchFocusModel new];
            [model setValuesForKeysWithDictionary:obj];
            NSMutableArray * array = [NSMutableArray array];
            for (NSInteger i = 0 ; i< model.imageNum; i++) {
                [array addObject:[Utilis getRandomDynamicImage]];
            }
            model.imageArray = array;
            [_dataArray addObject:model];
        }];

    }
    return _dataArray;
}
@end
